package com.teame.readbaby;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.teame.readbaby.BaseActivity.DataCallback;
import com.teame.readbaby.parser.Cart_AddupParser;
import com.teame.readbaby.parser.ShoppingCarListParser;
import com.teame.readbaby.po.Product;
import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.util.DptAsyncTask;
import com.teame.readbaby.util.DptAsyncTask.DptListener;
import com.teame.readbaby.util.GetImageUtil;
import com.teame.readbaby.util.Logger;
import com.teame.readbaby.vo.Cart_addup;

import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ShoppingCartActivity2 extends BaseActivity {

	private TextView shopcar_total_buycount_text_1;//数量总计
	private TextView shopcar_total_bonus_text_1;//赠送积分总计
	private TextView shopcar_total_money_text_1;//金额总计
	
	private ListView shopcar_product_list;
	
	private String Tag="ShoppingCartActivity2";
	
	private List<Product> list;
	private MyAdapter adapter;
	
	private LinearLayout shopcar_body_layout;
	private RelativeLayout shop_nopro;
	
	
	@Override
	protected void onClickEvent(View paramView) {
		
	}

	@Override
	protected void findViewById() {
		shopcar_total_buycount_text_1=(TextView) this.findViewById(R.id.shopcar_total_buycount_text_1);
		shopcar_total_bonus_text_1=(TextView) this.findViewById(R.id.shopcar_total_bonus_text_1);
		shopcar_total_money_text_1=(TextView) this.findViewById(R.id.shopcar_total_money_text_1);
		shopcar_product_list=(ListView) findViewById(R.id.shopcar_product_list);
		
		shopcar_body_layout=(LinearLayout) findViewById(R.id.shopcar_body_layout);
		shop_nopro=(RelativeLayout) findViewById(R.id.shop_nopro);
	}

	@Override
	protected void loadViewLayout() {
		
		setContentView(R.layout.shopping_car_activity_2);
	}

	@Override
	protected void processLogic() {
		
		//1,请求的url地址
				String url=ConstantValue.HOST+"/cart";
				//2,请求的参数，没有为null
				//如果有参数 这样写
				Map<String, String> params=new HashMap<String, String>();
				params.put("sku", "1200001\\:3|1200004\\:2");
				//sku=1200001:3|1200004:2
				//3.json数据解析器，大家需要按照自己获取json的结构自己编写，要继承baseParser
				//4,回调函数
				
				this.getDataFromServer(url, params, new ShoppingCarListParser(),new DataCallback<List<Product>>() {
					

					@Override
					public void processData(List<Product> paramObject,
							boolean paramBoolean) {
						
						
						if(!paramBoolean||paramObject == null){//获取失败
							
						}else{
							
							list=paramObject;
							
							int sum=0;
							for(Product info :paramObject){
								sum+=Integer.parseInt(info.getProdNum());
								//Logger.i(TAG, info.toString());
							}
							
							//设置总数量
							shopcar_total_buycount_text_1.setText(sum+"个");
							adapter = new MyAdapter();
							shopcar_product_list.setAdapter(adapter);
						}
						
					}
				});
				//获取总金额
		
				this.getDataFromServer(url, params, new Cart_AddupParser(),new DataCallback<Cart_addup>() {
					@Override
					public void processData(Cart_addup paramObject,
							boolean paramBoolean) {
						
						
						if(!paramBoolean||paramObject == null){//获取失败
							
						}else{
							
							//赠送积分
							shopcar_total_bonus_text_1.setText(paramObject.getTotal_point());
							//金额总计
							shopcar_total_money_text_1.setText(paramObject.getTotal_price());
						}
						
					}
				});
	}
	
	private class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(final int p, View view, ViewGroup arg2) {
			
			ViewHolder holder =null;
			if(view==null){
				holder= new ViewHolder();
				view=View.inflate(ShoppingCartActivity2.this, R.layout.shopping_car_listitem_2, null);
				
				holder.iv_icon=(ImageView) view.findViewById(R.id.shopcar_item_prodImage_img_s);
				holder.tv_prodname=(TextView)view. findViewById(R.id.shopcar_item_prodName_text_s);
				holder.tv_proid=(TextView) view.findViewById(R.id.shopcar_item_prodId_text_s);
				holder.tv_prodPrice=(TextView)view. findViewById(R.id.shopcar_item_prodPrice_text_s);
				holder.tv_prodCount=(TextView) view.findViewById(R.id.shopcar_item_prodCount_text_s);
				holder.tv_subtotal=(TextView)view. findViewById(R.id.shopcar_item_subtotal_text_s);
				holder.tv_del=(TextView)view. findViewById(R.id.shopcar_item_delete_text_s);
				
				view.setTag(holder);
			}else{
				holder=(ViewHolder) view.getTag();
			}
			
			Product product = list.get(p);
			
//			for(Product info :list){
//				//sum+=Integer.parseInt(info.getProdNum());
//				Logger.i(TAG, info.toString());
//			}
			
			//这里应该用异步
			
			
			holder.iv_icon.setImageBitmap(GetImageUtil.getBitmap(product.getPic()));
			holder.tv_prodname.setText(product.getName());
			holder.tv_proid.setText(product.getId());
			holder.tv_prodPrice.setText(product.getPrice());
			holder.tv_prodCount.setText(product.getNumber());
			holder.tv_subtotal.setText(product.getSubtotal());
//			
			holder.tv_del.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					list.remove(p);
					adapter.notifyDataSetChanged();
					
					if(list.size()==0){
						shop_nopro.setVisibility(View.VISIBLE);
						shopcar_body_layout.setVisibility(View.INVISIBLE);
					}
				}
			});
			
			Logger.i(TAG, list.size()+"ddddddddddddd");
			
			return view;
		}
		
	}
	
	private static class ViewHolder{
		ImageView iv_icon;
		TextView tv_prodname;
		TextView tv_proid;
		TextView tv_prodPrice;
		TextView tv_prodCount;
		TextView tv_subtotal;
		
		TextView tv_del;
	}
	

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub

	}

}
